package com.hzgj.bcl.soa.command;

import com.alibaba.fastjson.JSON;
import com.hzgj.bcl.soa.merge.MergeControl;
import com.hzgj.bcl.soa.merge.MergeControlResult;
import com.hzgj.bcl.util.IocContext;

/**
 * Created by warning on 2016/12/1.
 */
public class CliMergeCommand extends BaseCommand {

    MergeControl mergeControl;

    @Override
    public String invoke(CommandOptions commandOptions) {
        if (mergeControl == null) {
            return JSON.toJSONString(MergeControlResult.builder().success(false).detail("can't get MergeControl instance.").build());
        }
        String[] merge_param = commandOptions.params.get("drain");
        if (merge_param != null) {
            MergeControlResult mergeControlResult = mergeControl.drainMerge();
            return JSON.toJSONString(mergeControlResult);
        } else {
            String[] executor = commandOptions.params.get("executor");
            if (executor != null) {
                JSON.toJSONString(mergeControl.getExecutorTask());
            }
        }
        return "";
    }

    @Override
    public String getName() {
        return "cliMerge";
    }

    @Override
    public void start() {
        mergeControl = IocContext.applicationContext.getBean(MergeControl.class);
    }
}
